﻿// Copyright (c) 2014 Bryan C. O'Connell
// http://wsubi.codeplex.com/license
// Purpose: This class takes commands (orders) from the user, runs the specified script 
// or performs the requested action (prepares), then provides the results (serves).

using System;
using System.Collections.Generic;
using Wsubi.Code.Commands;
using Wsubi.Code.Utilities;

namespace Wsubi.Code
{
    class Chef
    {
        private string _ScriptsFolder;
        private ICommand _CurrentCommand;
        private ScriptData _ScriptData = new ScriptData();
        private ResultSet _Results = new ResultSet();

        public Chef(string ScriptsFolder)
        {
            Guard.ConfirmFolderExists(ScriptsFolder);
            _ScriptsFolder = ScriptsFolder;
            _ScriptData.BuildDataStores(_ScriptsFolder);
        }

        public void TakeOrder(string UserCommand)
        {
            CommandFactory Factory = new CommandFactory(UserCommand);
            _CurrentCommand = Factory.MakeCommand();            
        }

        public void PrepareOrder() 
        {
            _CurrentCommand.ScriptDataStorage = _ScriptData;
            _Results = _CurrentCommand.Execute();

            RebuildScriptDataIfNeeded();
        }

        // ServerOrder - List<string> or whole ResultSet?

        /// <remarks>Roll regathers information on the scripts which needs
        /// to replace the information currently being stored.</remarks>
        private void RebuildScriptDataIfNeeded()
        {
            if (_CurrentCommand is Roll) {
                _ScriptData = _CurrentCommand.ScriptDataStorage;
            }
        }
    }
}
